const staffService = require('@/services/staff.service')
const JsonResult = require('@/utils/httpResponse.util')
const permissionService = require('@/services/permission.service')

class StaffController {
  async findPage (req, response) {
    const { pageNo, pageSize, status, email, tel, searchKeywords, username, nickname } = req.query
    try {
      const data = await staffService.findPage(pageNo, pageSize, {
        searchKeywords, email, tel, status, username, nickname
      })
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async staffLogin (req, response) {
    const { username, password } = req.body
    try {
      const data = await staffService.login(username, password)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        data,
        message: '登录成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '登录失败' })
    }
  }

  async createStaff (req, response) {
    const { username, password, nickname, gender, status, avatar, email, tel, age, roleIds } = req.body
    try {
      const data = await staffService.create({ username, password, nickname, gender, status, avatar, email, tel, age })
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      try {
        const permissiondata = await permissionService.staffAllocationRole(data, roleIds)
        if (permissiondata?.errorMessage) {
          // 创建员工的时候如果角色分配失败删除刚才创建的员工
          staffService.delete(data)
          return JsonResult.fail({ req, response, message: permissiondata.errorMessage })
        }
      } catch (error) {
        // 创建员工的时候如果角色分配失败删除刚才创建的员工
        staffService.delete(data)
        return JsonResult.fail({ req, response, error, message: '创建员工失败' })
      }
      JsonResult.success({
        req,
        response,
        message: '创建员工成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '创建员工失败' })
    }
  }

  async updateStaff (req, response) {
    const { id, roleIds, ...attrs } = req.body
    try {
      const data = await staffService.update(id, attrs)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      const permissiondata = await permissionService.staffAllocationRole(id, roleIds)
      if (permissiondata?.errorMessage) {
        return JsonResult.fail({ req, response, message: permissiondata.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '更新员工成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新失败' })
    }
  }

  async updateStatus (req, response) {
    const { id, status } = req.body
    try {
      const data = await staffService.updateStatus(id, status)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '更新状态成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新状态失败' })
    }
  }

  async batchDelete (req, response) {
    const ids = req.body.ids
    try {
      const data = await staffService.batchDelete(ids)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  /**
   * 修改员工密码
   * @param {*} req
   * @param {*} response
   */
  async changePassword (req, response, selfChange) {
    const { username, password, oldPassword } = req.body
    try {
      const data = await staffService.changePassword({ username, password, oldPassword, selfChange })
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '密码修改成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '修改密码失败' })
    }
  }
}

module.exports = new StaffController()
